var map;
var game;

var image1Id = 2130837508;
var image2Id = 2130837508;
var image3Id = 2130837514;

/**
 * Extending the Game Object
 * 
 */

function Player(login, points, imageId, numberOfItems, marker) {
	this.login = login;
	this.points = points;
	this.imageId = imageId;
	this.numberOfItems = numberOfItems;
	this.marker = marker;
}

function Message(id, senderLogin, receiverLogin, messageBody) {
	this.id = id;
	this.senderLogin = senderLogin;
	this.receiverLogin = receiverLogin;
	this.messageBody = messageBody;
}

Game.prototype.players = new Array();
Game.prototype.getPlayer = function(login) {
	$.each(game.players, function(n, player) {
		if (player.login == login) {
			return player;
		}
	});
};
var messageWindow;

/**
 * Paths to item images
 */
var coinImage = '${COIN_IMG}';
var face1Image = '${FACE1_IMG}';
var face2Image = '${FACE2_IMG}';
var face3Image = '${FACE3_IMG}';
var questionImage = '${QUESTION_IMG}';
var bonusImage = '${BONUS_IMG}';

var latitude = '${LATITUDE}';
var longitude = '${LONGITUDE}';

var period = '${PERIOD}';
var watchURL = '${WATCH_URL}';

var messageWindows = new Array();

$(document).ready(function() {
	/**
	 * Map initialization
	 */
	var latlng = new google.maps.LatLng(latitude/1000000, longitude/1000000);
	var myOptions = {
		zoom: 12,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true,
		navigationControl: true,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.ZOOM_PAN,
			position: google.maps.ControlPosition.LEFT
		},
	}
	map = new google.maps.Map(document.getElementById('mapCanvas'), myOptions);
	google.maps.event.addListener(map, 'click', function(event) {
   		doOnClick(event);
	});
	
	game = new Game();
	aId = ${gameId};
	cycle();
	
});

function cycle() {
		$.ajax({
			url : watchURL + aId,
			type : 'GET',
			dataType: "xml",
			success: parseResponse
		});
		setTimeout(cycle, period);
}
function parseResponse(xml) {
	removeItems();
	removePlayers();
	removeInfoWindows();
	
	$(xml).find("item").each(function() {
		
		id = $(this).find("itemId").text();
		latitude = $(this).find("latitude").text();
		longitude = $(this).find("longitude").text();
		points = $(this).find("points").text();
		type = $(this).find("type").text();
		latLng = new google.maps.LatLng(latitude * 0.000001, longitude * 0.000001, true);
		marker = placeMarker(latLng, type == 'COI' ? coinImage : (type == 'QUE' ? questionImage : bonusImage));
		item = new Coin(id, points, marker);
		game.items[game.items.length] = item;
		game.items.length++;
	});
	$(xml).find("inGamePlayerInfo").each(function() {
		login = $(this).find("login").text();
		latitude = $(this).find("latitude").text();
		longitude = $(this).find("longitude").text();
		points = $(this).find("points").text();
		numberOfItems = $(this).find("numberOfItems").text();
		imageId = $(this).find("imageId").text();
		
		latLng = new google.maps.LatLng(latitude * 0.000001, longitude * 0.000001, true);
		marker = placeMarker(latLng, imageId == image1Id ? face1Image : imageId == image2Id ? face2Image : face3Image);
		var player = new Player(login, points, imageId, numberOfItems, marker);
		game.players[game.players.length] = player;
		game.players.length++;
	});
	$(xml).find("message").each(function() {
		id = $(this).find("messageId").text();
		sender = $(this).find("senderLogin").text();
		receiver = $(this).find("receiverLogin").text();
		messageBody = $(this).find("body").text();
		player = getPlayer(sender);
		messageWindow = new google.maps.InfoWindow({
			disableAutoPan : true
		});
		messageWindow.setContent(getMessageContentHtml(receiver, messageBody));
	    messageWindow.open(map, player.marker);
	    messageWindows[messageWindows.length] = messageWindow;
	    messageWindows.length++;
	});
	$(xml).find("gameInfo").each(function() {
		city = $(this).find("city").text();
		pointsToWin = $(this).find("pointsToWin").text();
		maxPlayers = $(this).find("maxPlayers").text();
		$('#citySpan').text(city);
		$('#pointsToWinSpan').text(pointsToWin);
		$('#activePlayersSpan').text(getNumberOfPlayers());
		$('#maxPlayersSpan').text(maxPlayers);
	});
	updateRanking();
}
function updateRanking() {
	$('.rankingTableRow').remove();
	
	game.players.sort(function(a, b) {
	   var compA = a.points;
	   var compB = b.points;
	   return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
	});
	
	$.each(game.players, function(idx, player) {
		if (player != null) {
			$('#rankingTable').append(
					'<tr class="rankingTableRow" id="row_' + player.login + '">' +
					getPlayerRankingRowContentHtml(player) + '</tr>');
		}
	});
}
function getPlayerRankingRowContentHtml(player) {
	return '<td>' + player.login + '<div class="itemIcon"><img src="' + player.marker.icon +'"/></div></td>' +
		'<td>' + player.numberOfItems + '</td>' +
		'<td>' + player.points + '</td>';
}
function removeNonPresentItems(presentItemIds) {
	
}
function getMessageContentHtml(receiverLogin, messageBody) {
	result = '<div class="messageReceiver">To <span class="login" >' + receiverLogin + '</span> : </div>';
	result = result + '<span class="messageBody">' + messageBody + '</span>';
	return result;
}
function getPlayer(login) {
	for (var i in game.players) {
		if (game.players[i] != null && game.players[i].login == login) {
			return game.players[i];
		}
	}
	return null;
}
function getNumberOfPlayers() {
	var result = 0;
	for (var i in game.players) {
		if (game.players[i] != null) {
			result++;
		}
	}
	return result;
}
function getItem(id) {
	for (var i in game.items) {
		if (game.items[i] != null && game.items[i].id == id) {
			return game.items[i];
		}
	}
	return null;
}
function removeInfoWindows() {
	for (var i in messageWindows) {
		if (messageWindows[i] != null) {
			messageWindows[i].close();
			messageWindows[i] = null;
		}
	}
	messageWindows.length = 0;
}
function removePlayers() {
	for (var i in game.players) {
		game.players[i].marker.setMap(null);
		game.players[i].marker = null;
		game.players[i] = null;
	}
	game.players.length = 0;
}
function removeItems() {
	for (var i in game.items) {
		game.items[i].marker.setMap(null);
		game.items[i].marker = null;
		game.items[i] = null;
	}
	game.items.length = 0;
}
function updatePlayer(login, points, numberOfItems, marker) {
	player = game.getPlayer(login);
	player.points = points;
	player.numberOfItems = numberOfItems;
	player.marker = marker;
}
function doOnClick(event) {
}
function placeMarker(position, image) {
	var marker = new google.maps.Marker({
		position : position, 
		draggable : false,
		map : map,
		clickable : true,
		icon : image,
	});
	
	google.maps.event.addListener(marker, 'click', function() {

	});
	return marker;
}
